//
//  LHAVPlayerManager.m
//  EPA
//
//  Created by 卢浩 on 16/4/21.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHAVPlayerManager.h"
#import "LHRadioDetailModel.h"
#import "LHMenuViewController.h"
#import "LHRadioDetailViewController.h"

@implementation LHAVPlayerManager

+ (instancetype)shareInstant{
    static LHAVPlayerManager *manager = nil;
    @synchronized (self) {
        if (manager == nil) {
            manager = [[LHAVPlayerManager alloc]init];
            manager.avPlayer = [[AVPlayer alloc]init];
            manager.currentIndex = NSIntegerMax;
            [manager addTimer];
        }
    }
    return manager;
}

- (void)play{
    [self.avPlayer play];
}

- (void)pause{
    [self.avPlayer pause];
}

- (void)last{
    self.currentIndex --;
    if (self.currentIndex < 0) {
        self.currentIndex = self.itemsArray.count - 1;
    }
//    NSLog(@"%ld",self.currentIndex);
    [LHRadioDetailViewController replaceTitleAndImageViewAndWebView];
    [self seekToTime:CMTimeMake(0, 1)];
    [self replaceMusic];
}

- (void)next{
    self.currentIndex ++;
    if (self.currentIndex > self.itemsArray.count - 1) {
        self.currentIndex = 0;
    }
//    NSLog(@"%ld",self.currentIndex);
    [LHRadioDetailViewController replaceTitleAndImageViewAndWebView];
    [self seekToTime:CMTimeMake(0, 1)];
    [self replaceMusic];
}

- (void)addTimer{
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

- (void)timerAction{
    [self setLeftMenuMusicView];
    
    //当前时间
    CGFloat currentTime = 0;
    if (self.avPlayer.currentTime.timescale > 0) {
        currentTime = self.avPlayer.currentTime.value / self.avPlayer.currentTime.timescale;
    }
    //总时间
    CGFloat allTime = 0;
    if (self.avPlayer.currentItem.duration.timescale != 0) {
        allTime = self.avPlayer.currentItem.duration.value / self.avPlayer.currentItem.duration.timescale;
    }
    
    if (self.avPlayer.rate == 0) {
        if (currentTime == allTime) {
            [self seekToTime:CMTimeMake(0, 1)];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [LHRadioDetailViewController nextMusic];
            });
        }
    }
}

- (void)replaceMusic{
//    NSLog(@"%ld",self.currentIndex);
    [self replaceAVPlayerWithItem:self.itemsArray[self.currentIndex]];
    [self.avPlayer play];
}

- (void)seekToTime:(CMTime)time{
    [self.avPlayer seekToTime:time];
}

- (void)replaceAVPlayerWithItem:(AVPlayerItem *)item{
//    NSLog(@"%@",item);
    [self.avPlayer replaceCurrentItemWithPlayerItem:item];
}

- (void)setLeftMenuMusicView{
    if (self.radioDetailArray.count != 0 && self.currentIndex < self.radioDetailArray.count) {
        LHRadioDetailModel *model = self.radioDetailArray[self.currentIndex];
        [[LHMenuViewController getMenuViewController] setMusicViewWithTitle:model.title Desc:model.userinfo[@"uname"] Img:model.imgUrl];
    }
}

@end
